home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 3751 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.9 KB  |  81 lines

  1. Path: newshost.lanl.gov!tanmoy
  2. From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: How to give functions as parameters?
  5. Date: 31 Jan 1996 00:13:07 GMT
  6. Organization: Los Alamos National Laboratory
  7. Message-ID: <TANMOY.96Jan30171307@qcd.lanl.gov>
  8. References: <Pine.SUN.3.91N2x.960130222756.11581A-100000@yellow59.nada.kth.se>
  9. NNTP-Posting-Host: qcd.lanl.gov
  10. Mime-Version: 1.0
  11. Content-Type: text
  12. In-reply-to: Per Steneskog's message of Tue, 30 Jan 1996 22:42:30 +0100
  13.  
  14. In article
  15. <Pine.SUN.3.91N2x.960130222756.11581A-100000@yellow59.nada.kth.se> Per
  16. Steneskog <md94-pst@nada.kth.se> writes: 
  17. <snip>
  18.    I have a little problem, I dont know how to send a fuction,
  19.    as a parameter to another function.. 
  20.  
  21. Just as would you send a variable as a parameter to another function? You
  22. name it!
  23.  
  24.    void
  25.    do_it(void (*it)())
  26.  
  27. It is better style to declare it as void do_it(void (*it)(void))
  28. instead: always use a prototype if you can.
  29.  
  30. <snip>
  31.    void
  32.    do_me(
  33.      void)
  34. <snip>
  35.    int
  36.    main(
  37.     void)
  38.    {
  39.      do_it(do_me()); /* ERROR-COMPILE LINE */
  40.    }
  41.  
  42. What are the () doing after the do_me? () means `call the
  43. previous and take the result'. You certainly do not want to pass the
  44. result of calling do_me! you want to pass do_me. So, just say
  45. do_it(do_me) ... heck, I would prefer different names :-)
  46.  
  47. I do not understand why functions are mor confusing than arrays: I
  48. have never seen some one try to pass an array declared as int a[5] as
  49. `a[5]' or `a[]'. Why then the confusion about functions?
  50.  
  51. By the way, if you think about the answer hard enough, you would
  52. discover another puzzle: You declare do_it as accepting a pointer to a
  53. function, and you pass it a function instead! It turns out that when a
  54. function (except in some illegal contexts) always `decays' to its
  55. address: so you could just as well have written do_it(&do_me). (Of
  56. course, it also implies that you can write
  57. (&*******do_it)(***&****&**do_me) if you really wanted to be
  58. perverse.) A related point is that you can declare a function as
  59. taking another function: the meaning is identical to the situation
  60. where you declare it as taking a pointer to a function.
  61.  
  62.    Short explaination: From main(), do_it() should be called with
  63.    the parameter (and function) do_me(). (Pretty obvious :)
  64.  
  65.    When I try to compile this, i got the following error message: (gcc)
  66.        In function `main':
  67.        invalid use of void expression
  68.  
  69. because you had asked the compiler to return the value of calling
  70. do_me to do_it, but do_me does not return a value!
  71.  
  72. Cheers
  73. Tanmoy
  74. --
  75. tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
  76. Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
  77. Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
  78. <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
  79. internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
  80. fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]
  81.